package com.hsy.linemoney.modules.my.mydeals;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.deparse.dpframe.tools.utils.DPLogUtil;
import com.hsy.linemoney.R;
import com.hsy.linemoney.apiservice.Callback;
import com.hsy.linemoney.apiservice.MyDealService;
import com.hsy.linemoney.modules.common.CommonBaseFragment;
import com.hsy.linemoney.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 等待交易
 * Created by Administrator on 2016/7/11.
 */
public class WaitDealFragment extends CommonBaseFragment {

    private MyDealService service = new MyDealService();
    private Adapter adapter;
    private List<MyDealService.NewOrderResult.NewOrders> listData = new ArrayList<>();
    private LayoutInflater inflater;
    private ListView listView;

    @Override
    protected int getFragmentLayout() {
        return R.layout.fragment_my_deal;
    }

    @Override
    protected void initView(View view, Bundle bundle) {
        inflater = LayoutInflater.from(getAttchedActivity());
        listView = F(R.id.listView);

        showProgressDialog(getAttchedActivity());
        service.getNewOrders(new Callback<MyDealService.NewOrderResult>() {
            @Override
            public void onSuccess(int what, MyDealService.NewOrderResult result) {
                if (null != result.getData().getNewOrders()) {
                    listData = result.getData().getNewOrders();
                    adapter = new Adapter();
                    listView.setAdapter(adapter);
                }
                hideProgressDialog();
            }

            @Override
            public void onError(int what, int type, String msg, Exception e) {
                hideProgressDialog();
                DPLogUtil.d("onError");
            }
        }, true);
    }

    private class Adapter extends BaseAdapter {

        @Override
        public int getCount() {
            return listData.size();
        }

        @Override
        public Object getItem(int position) {
            return listData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            convertView = inflater.inflate(R.layout.deal_list_item, null);
            View listIcon = (View) convertView.findViewById(R.id.list_icon);
            TextView orderSn = (TextView) convertView.findViewById(R.id.order_sn);
            TextView pubTime = (TextView) convertView.findViewById(R.id.pub_time);
            TextView waitMoney = (TextView) convertView.findViewById(R.id.wait_money);
            TextView exchangeRate = (TextView) convertView.findViewById(R.id.exchange_rate);
            TextView expectedMoney = (TextView) convertView.findViewById(R.id.expected_money);
            TextView dealTime = (TextView) convertView.findViewById(R.id.deal_time);
            View dealTimeContainer = convertView.findViewById(R.id.deal_time_container);
            listIcon.setVisibility(View.GONE);
            orderSn.setVisibility(View.GONE);
            dealTimeContainer.setVisibility(View.GONE);

            pubTime.setText(listData.get(position).getPostingDate()+getString(R.string.publish));
            waitMoney.setText(String.valueOf(listData.get(position).getFromAmount()) + listData.get(position).getFromCurrencyCd());
            exchangeRate.setText(String.valueOf(listData.get(position).getFxRate()));
            expectedMoney.setText(listData.get(position).getEstimatedAmount());

            return convertView;
        }
    }
}
